﻿using DownloadBook.Entity;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DownloadBook {
    public partial class FrmProxySetting : Form {
        protected ProxyConfig proxyConfig;
        public FrmProxySetting(ProxyConfig proxyConfig) {
            InitializeComponent();

            this.proxyConfig = proxyConfig;
        }

        private void FrmProxySetting_Load(object sender, EventArgs e) {
            //启用状态
            this.chkEnabled.Checked = this.proxyConfig.Enabled;

            //主机
            this.txtHostName.Text = this.proxyConfig.HostName;
            this.txtPort.Text = "" + this.proxyConfig.Port;

            //身份验证
            this.chkNeedAuthentication.Checked = this.proxyConfig.NeedAuthentication;
            this.txtUserName.Text = this.proxyConfig.UserName;
            this.txtPassowrd.Text = this.proxyConfig.Passwrod;

            //代理类型
            this.comProxyType.Items.AddRange(Enum.GetNames(typeof(ProxyType)));
            this.comProxyType.Text = Enum.GetName(typeof(ProxyType), this.proxyConfig.ProxyType);

            //更新UI的启用状态
            this.UpdateEnabledStateUI();
        }
        

        /// <summary>
        /// 复选框 - 代理启用状态
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ChkEnabled_CheckedChanged(object sender, EventArgs e) {
            //更新UI的启用状态
            this.UpdateEnabledStateUI();
        }

        /// <summary>
        /// 复选框 - 身份验证
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ChkNeedAuthentication_CheckedChanged(object sender, EventArgs e) {
            //更新UI的启用状态
            this.UpdateEnabledStateUI();
        }

        /// <summary>
        /// 输入框 - 端口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TxtPort_KeyPress(object sender, KeyPressEventArgs e) {
            if ((e.KeyChar < '0' || e.KeyChar > '9')) {//允许输入退格键
                if(e.KeyChar != (char)Keys.Back) {
                    e.Handled = true;
                }
            }
        }
        /// <summary>
        /// 按钮 - 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnSave_Click(object sender, EventArgs e) {
            //主机
            if (!int.TryParse(this.txtPort.Text, out int port)) {
                MessageBox.Show("端口号不正确，请重新输入", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            this.proxyConfig.Port = port;
            this.proxyConfig.HostName = this.txtHostName.Text;

            //启用状态
            this.proxyConfig.Enabled = this.chkEnabled.Checked;

            //身份验证
            this.proxyConfig.NeedAuthentication = this.chkNeedAuthentication.Checked;
            this.proxyConfig.UserName = this.txtUserName.Text;
            this.proxyConfig.Passwrod = this.txtPassowrd.Text;

            //代理类型
            Array proxyTypes = Enum.GetValues(typeof(ProxyType));
            int proxyTypeIndex = this.comProxyType.SelectedIndex;
            this.proxyConfig.ProxyType = (ProxyType)proxyTypes.GetValue(proxyTypeIndex);

            //保存到文件
            ProxyConfig.SaveToFile(this.proxyConfig);

            //关闭窗口
            this.Close();
        }

        /// <summary>
        /// 按钮 - 关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnClose_Click(object sender, EventArgs e) {
            this.Close();
        }
        

        /// <summary>
        /// 更新 UI 的启用状态
        /// </summary>
        protected void UpdateEnabledStateUI() {
            bool enabledProxy = this.chkEnabled.Checked;

            //代理类型
            this.comProxyType.Enabled = enabledProxy;

            //主机
            this.txtHostName.Enabled = enabledProxy;
            this.txtPort.Enabled = enabledProxy;

            //身份验证
            bool needAuthentication = enabledProxy && this.chkNeedAuthentication.Checked;
            this.chkNeedAuthentication.Enabled = enabledProxy;
            this.txtUserName.Enabled = needAuthentication;
            this.txtPassowrd.Enabled = needAuthentication;
        }
    }
}
